package com.tes;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.modifier.PathModifier.Path;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.opengl.vertex.RectangleVertexBuffer;

import com.tes.GameResource.PlayerDirection;

public class AnimateObject extends AnimatedSprite {

	public long[] duration = { 100, 100, 100, 100 };
	public PhysicsHandler handler;
	public float speed;
	public Path path;
	public int position;

	private PlayerDirection direction = PlayerDirection.NONE;

	public void createPath(Path path) {
		this.path = path;
		this.position = 0;
	}

	public void createPhysic(float speed) {
		this.speed = speed;
		this.handler = new PhysicsHandler(this);
		this.handler.setVelocity(0);
		this.registerUpdateHandler(this.handler);
	}

	public void createModifier() {
		IUpdateHandler updateHandler = new IUpdateHandler() {

			@Override
			public void reset() {

			}

			@Override
			public void onUpdate(float pSecondsElapsed) {
				int size = path.getSize() - 1;

				if (position < size) {
					float x = path.getCoordinatesX()[position]
							- path.getCoordinatesX()[position + 1];
					float y = path.getCoordinatesY()[position]
							- path.getCoordinatesY()[position + 1];

					float px = 0;
					float py = 0;

					if (y < 0) {
						if (direction != PlayerDirection.DOWN) {
							AnimateObject.this.animate(duration, 0, 3, true);
							direction = PlayerDirection.DOWN;
						}
						
						py = 1;
					} else if (y > 0) {
						if (direction != PlayerDirection.UP) {
							AnimateObject.this.animate(duration, 12, 15, true);
							direction = PlayerDirection.UP;
						}
						
						py = -1;
					} else if (x > 0) {
						if (direction != PlayerDirection.LEFT) {
							AnimateObject.this.animate(duration, 4, 7, true);
							direction = PlayerDirection.LEFT;
						}
						
						px = -1;
					} else if (x < 0) {
						if (direction != PlayerDirection.RIGHT) {
							AnimateObject.this.animate(duration, 8, 11, true);
							direction = PlayerDirection.RIGHT;
						}

						px = 1;
					} else {
						AnimateObject.this.stopAnimation();
						direction = PlayerDirection.NONE;
					}

					handler.setVelocity(px * speed, py * speed);

					float dx = path.getCoordinatesX()[position + 1];
					float dy = path.getCoordinatesY()[position + 1];

					if (AnimateObject.this.getX() >= (dx - 1)
							&& AnimateObject.this.getX() <= (dx + 1)) {
						if (AnimateObject.this.getY() >= (dy - 1)
								&& AnimateObject.this.getY() <= (dy + 1)) {
							position += 1;
							// AnimateObject.this.setPosition(dx, dx);
						}
					}
				} else {
					position = 0;
					AnimateObject.this.setInitialPosition();
				}
			}
		};

		this.registerUpdateHandler(updateHandler);
	}

	public AnimateObject(float pX, float pY,
			TiledTextureRegion pTiledTextureRegion,
			RectangleVertexBuffer pRectangleVertexBuffer) {
		super(pX, pY, pTiledTextureRegion, pRectangleVertexBuffer);
	}

	public AnimateObject(float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTiledTextureRegion);
	}

	public AnimateObject(float pX, float pY, float pTileWidth,
			float pTileHeight, TiledTextureRegion pTiledTextureRegion,
			RectangleVertexBuffer pRectangleVertexBuffer) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion,
				pRectangleVertexBuffer);
	}

	public AnimateObject(float pX, float pY, float pTileWidth,
			float pTileHeight, TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion);
	}

}
